<?php

namespace app\admin\controller\card;

use app\admin\model\User;
use app\common\controller\Backend;
use fast\Random;

/**
 * 卡密管理
 *
 * @icon fa fa-circle-o
 */
class Code extends Backend
{

    /**
     * Code模型对象
     * @var \app\admin\model\card\Code
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\card\Code;
        $this->view->assign("isExchangeList", $this->model->getIsExchangeList());
    }

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user','agent'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);
            foreach ($list as $row) {
                
                
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    
    public function create()
    {
        if ($this->request->isPost()) {
            
            $num = $this->request->param('num', 0);
            if ($num == 0) {
                $this->error('请输入生成数量');
            }
            $agent_id = $this->request->param('agent_id', 0);
            if ($agent_id == 0) {
                $this->error('请选择所属代理');
            }
           
           
            $ins_dastas = [];
            for ($i = 0; $i < $num; $i++) {
                $ins_dastas[$i] = ['card' => date('Ymd').rand(111111,999999), 'code' => Random::alnum(8), 'create_time' => time(), 'agent_id' => $agent_id];
            }
            $this->model->insertAll($ins_dastas);

            $this->success('成功');
        }
        return $this->view->fetch();
    }

    public function transfer()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!$start = \app\admin\model\card\Code::get($data['start_no'])) {
                $this->error('开始序号不存在');
            }
            if (!$end = \app\admin\model\card\Code::get($data['end_no'])) {
                $this->error('结束序号不存在');
            }
            if (!$agent = User::get(['id' => $data['agent_id']])) {
                $this->error('代理不存在');
            } elseif (!$agent['is_agent']) {
                $this->error('该账户身份不是代理，请检查');
            }
            $codeIds = \app\admin\model\card\Code::where('is_exchange', 0)->where('id', '>=', $start['id'])->where('id', '<=', $end['id'])->column('id');
            if (!count($codeIds)) {
                $this->error('没有符合条件的卡密');
            }
            \app\admin\model\card\Code::whereIn('id', $codeIds)->update(['agent_id' => $agent['id']]);
            $this->success('操作成功');
        } else {
            return $this->fetch();
        }
    }
}
